/**
 * Dashboard Privacy
 * The object responsable for providing the privacy interface
 *
 * @author Seth
 * @version 0.3
 */
Dashboard.Privacy = function() {
	var perms = {0: 'public', 1: 'friends', 2: 'me', 3: 'custom'},
		increment = 103,
		open = false,

	// syncronize the zindex
	_sync = function() {
		$('div.entry span.privacy_wrap:not(span.privacy_loaded)').each(function(k, v) {
			var x = $(v),
				v = x.find('i:first').attr('class');

			// behave as buttons not text
			x.disableSelection();

			// correct zindex and set identity
			x.css({zIndex: increment});
			x.data({id: 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);})});
			x.addClass('privacy_loaded');
			increment--; if(increment < 90) increment = 103;

			// set the active class
			x.find('ul > li').each(function(k, d) {
				var d = $(d);

				d.removeClass('active');
				if(d.find('i').hasClass(v))
					d.addClass('active');
			});
		});

	// open privacy dropdown
	}, _open = function() {
		var x = $(this),
			y = open != false ? $('span.privacy_wrap:data("id='+ open +'")') : false;

		// close post privacy
		if(Dashboard.Post)
			Dashboard.Post.close();

		// already opened somewhere? close that.
		if(y) {
			if(open == x.data('id')) return _close(y);
			else _close(y);
		}

		// open
		x.addClass('privacy_down');
		x.find('ul').show();
		$(x.find('i').get(1)).removeClass().addClass('p_up');

		// set as opened
		open = x.data('id'); console.log(open);
		return false;

	// close privacy dropdown
	}, _close = function(x) {
		x.removeClass('privacy_down');
		x.find('ul').hide();
		$(x.find('i').get(1)).removeClass().addClass('p_down');

		open = false;

	// return permission (defaults to 0, public as per table default)
	}, findPerm = function(x) {
		var o = 0;
		$.each(perms, function(s, i){
			if(('p_'+ i) == x)
				o = s;
		});

		return o;

	// save privacy permissions
	}, _save_privacy = function() {
		var x = $(this),
			v = x.find('i').attr('class').replace(' margin', ''),
			b = x.closest('div.entry').data('id');

		// load custom dialog for custom state
		if(v == 'p_custom') {
			Dashboard.Privacy.Custom.init(x, b, findPerm(v), v);
			return true;
		}

		// skip save if we click the same state again
		if(x.hasClass('active'))
			return true;

		// save new state
    	Petolio.ajax({
			url: 'dashboard/privacy',
			type: 'post',
			data: {a: 'save', x: b, d: findPerm(v)},
			cache: false,
			success: function () {
				changeState(x, v);
			}
		});

	// change entity privacy state
	}, changeState = function(x, v) {
		var	y = x.closest('span').find('i').first(),
			z = x.closest('div.entry').find('div.privacy');

		// active highlight
		x.closest('ul').find('li').removeClass('active');
		x.addClass('active');

		// privacy icon
		y.fadeOut('fast', function() {
			y.removeClass().addClass(v).fadeIn('fast');
		});

		// entry icon
		z.fadeOut('fast', function() {
			z.removeClass().addClass('privacy').addClass(v).fadeIn('fast');
		});

	// event binder
	}, _bind = function() {
		// privacy events
		$("div.entries").delegate('span.privacy_wrap', 'click', _open);
		$("div.entries").delegate('span.privacy_wrap > ul > li', 'click', _save_privacy);
		$("html").delegate('body', 'click', function() {
			// close privacy boxes
			$('div.entry').find('span.privacy_wrap').each(function(k, v) {
				_close($(v));
			});
		});

	// constructor
	}, __construct = function() {
		_bind();
	};

	// public
	return {
		init: __construct,
		sync: _sync,
		changeState: changeState,
		findPerm: findPerm,
		close: _close,
		perms: function(x) {
			return perms[x];
		}
	};
}();